package day240200_winter_holiday_homework;

public class BinarySearch {
    public static void main(String[] args) {
        // 数据（有序）
        int[] arr = new int[]{3, 4, 5, 6, 7, 8, 9};
        int target = 5;

        int low = 0;
        int high = arr.length - 1;

        boolean found = false;

        while(low != high || arr[low] == target){
            int middle = (low + high) / 2;
            System.out.println("low=" + low + ", high=" + high + ", middle=" + middle);

            if(arr[middle] == target){
                System.out.println("在index=" + middle + "的位置上找到了" + target);
                found = true;
                break;
            }
            else if (arr[middle] > target){
                high = middle - 1;
            }
            else {
                low = middle + 1;
            }
        }

        if (!found){
            System.out.println("没有找到" + target);
        }
    }
}
